<script src="{{ getAssets }}js/bivariatetable/bivariatetable.js" type="text/javascript"></script>
<script type="text/javascript">
var goods_snd = {
    'sph_min' : -6,
    'sph_max' : 0,
    'sph_step':0.25,
    'cyl_min':-4,
    'cyl_max':0,
    'cyl_step':0.25
};

$(function(){
    var ranages = getQueryValue('ranges');
    if(ranages != null) {
        goods_snd = JSON.parse(ranages);
    }
    setBivariatetable(goods_snd, {});
});

$('#reslect_bivariatetable_len').click(function () {
    var uri = '{{ urlfor "AllocationController.BivariatetableLen" }}';
    layer.open({
        type: 2,
        title: '重选光度',
        shadeClose: false,
        shade: 0.2,
        maxmin: false,
        shift: 1,
        area: ['760px', '540px'],
        content: uri
    });
    return false;
});

function setBivariatetable(goods_snd, goods_detail) {
	var cyl_min = parseFloat(goods_snd.cyl_min);
	var cyl_max = parseFloat(goods_snd.cyl_max);
	var cyl_step = parseFloat(goods_snd.cyl_step);
	var sph_min =  parseFloat(goods_snd.sph_min);
	var sph_max =  parseFloat(goods_snd.sph_max);
	var sph_step =  parseFloat(goods_snd.sph_step);
	var x  = [];
	var y = [];
	var valueInfo = [];
	var valueJson={};
	if(cyl_min==cyl_max){
		var new_va =  formatnumber(cyl_min, "0.00");
		if(new_va>0){
			new_va = "+"+new_va;
		}else if(new_va==0){
			new_va  = new_va.toString()+".00";
		}
		x.push(new_va);
	}else{
		for(var i = cyl_min;i<=cyl_max;i=i+cyl_step){
			var new_va =  formatnumber(i, "0.00");
			if(new_va>0){
				new_va = "+"+new_va;
			}else if(new_va==0){
				new_va  = new_va.toString()+".00";
			}
			x.push(new_va);
		}
	}
	if(sph_min==sph_max){
		var new_va =  formatnumber(sph_min, "0.00");
		if(new_va>0){
			new_va = "+"+new_va;
		}else if(new_va==0){
			new_va  = new_va.toString()+".00";
		}
		y.push(new_va);
	}else{
		for(var i = sph_min;i<=sph_max;i=i+sph_step){
			var new_va =  formatnumber(i, "0.00");
			if(new_va>0){
				new_va = "+"+new_va;
			}else if(new_va==0){
				new_va  = new_va.toString()+".00";
			}
			y.push(new_va);
		}
	}
	
	if(sph_min<0&&sph_max<=0){
		y = y.sort(function(a,b){
			return b-a;
		});
	}
	if(cyl_min<0&&cyl_max<=0){
		x = x.sort(function(a,b){
			return b-a;
		});
	}
	/*
	for(var v in goods_detail){
		valueJson={};
		valueJson.sph = goods_detail[v].Snd.sph;
		valueJson.cyl = goods_detail[v].Snd.cyl ? goods_detail[v].Snd.cyl : goods_detail[v].Snd.add;
		valueJson.purchase_num = goods_detail[v].purchase_num
		valueInfo.push(valueJson);
	}*/
	$("#bivariate-table").bivariatetable({
		xaxis: x,
		yaxis: y,
		defaultposition: {cyl: x[0], sph: y[0]},
		defaultcolor: "#aaa",
		defaultbgcolors: "#fff",
		xkey: "cyl",
		ykey: "sph",
        reloads:true,
		valuekey: "purchase_num",
		data: valueInfo,
		onselect: function(callback) {

        }
	});
}
$('#confirmBivariatetable').click(function(){
    var data = $("#bivariate-table").bivariatetable().getData();
    console.log(data);return false;
    $.each(data, function (idx, val) {
        val.allocation_num = val.purchase_num;
        parent.confirmBivariatetable(val);
    });
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});

</script>